package tinyuser.acl;

import java.io.Serializable;
import java.security.Principal;
import java.security.acl.Permission;

/**
 * Holds the granted combintation of Principal/Permission in a {@link SimpleAcl}.
 * 
 * @author Martin Algesten
 * 
 */
@SuppressWarnings("serial")
public class SimpleAclEntry implements Serializable {

	private Principal principal;
	private Permission permission;



	/**
	 * Constructs an entry by providing principal and permission.
	 */
	public SimpleAclEntry(Principal principal, Permission permission) {
		if (principal == null)
			throw new IllegalArgumentException("principal can't be null");
		if (permission == null)
			throw new IllegalArgumentException("permission can't be null");
		this.principal = principal;
		this.permission = permission;
	}



	/**
	 * The principal that is granted a permission.
	 */
	public Principal principal() {
		return principal;
	}



	/**
	 * The permission granted to a principal.
	 */
	public Permission permission() {
		return permission;
	}



	@Override
	public String toString() {
		return "SimpleAclEntry[" + principal.getName() + "," + permission.toString() + "]";
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((permission == null) ? 0 : permission.hashCode());
		result = prime * result + ((principal == null) ? 0 : principal.hashCode());
		return result;
	}



	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SimpleAclEntry other = (SimpleAclEntry) obj;
		if (permission == null) {
			if (other.permission != null)
				return false;
		} else if (!permission.equals(other.permission))
			return false;
		if (principal == null) {
			if (other.principal != null)
				return false;
		} else if (!principal.equals(other.principal))
			return false;
		return true;
	}

}
